﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CustomUIControls;


using Entity;

namespace SalesVMT
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            helpProvider1.HelpNamespace = Application.StartupPath + "\\SalesVMT.chm";
            Program.taskbarNotifier = new TaskbarNotifier();
            Program.taskbarNotifier.SetBackgroundBitmap(SalesVMT.Properties.Resources.skin, Color.FromArgb(255, 0, 255));
            Program.taskbarNotifier.TitleRectangle = new Rectangle(40, 9, 70, 25);
            Program.taskbarNotifier.ContentRectangle = new Rectangle(8, 41, 133, 68);
            Program.taskbarNotifier.CloseClickable = false;
            Program.taskbarNotifier.TitleClickable = false;
            Program.taskbarNotifier.ContentClickable = false;
            Program.taskbarNotifier.EnableSelectionRectangle = false;
            Program.taskbarNotifier.KeepVisibleOnMousOver = true;
            Program.taskbarNotifier.ReShowOnMouseOver = false;
            tslTime.Text = "";
            tslUsers.Text = "";
            tslTask.Text = "";
            txtFullName.Text = "";
            panel1.Visible = false;
            panel2.Visible = false;
            DisplayFrame();
        }

            

        #region Functions

        private bool findMdiChildren(string formName)
        {
            bool val = false;
            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                if (this.MdiChildren[i].Name == formName)
                {
                    val = true;
                    this.MdiChildren[i].BringToFront();
                    break;
                }
            }
            return val;
        }
        private bool findMdiChildren(string formName, string formText)
        {
            bool val = false;
            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                if (this.MdiChildren[i].Name == formName && this.MdiChildren[i].Text == formText)
                {
                    val = true;
                    this.MdiChildren[i].BringToFront();
                    if (MessageBox.Show("Báo cáo này đã được mở, bạn cần phải đóng lại để nạp dữ liệu mới.\nBạn có chắc chắn đóng không?", "Thông báo", 
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        this.MdiChildren[i].Close();
                    break;
                }
            }
            return val;
        }


        #endregion

        private void mnuExit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                this.MdiChildren[i].Close();
            }
            this.Close();
        }

        private void mnuCompany_Click(object sender, EventArgs e)
        {
            Form f = new frmCompany(1);
            f.ShowDialog(this);
        }



        private void mnuOrderPurchase_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmOderPurchaseList"))
            {
                frmOderPurchaseList f = new frmOderPurchaseList();
                f.Show(dockPanel1);
            }
        }

        private void mnuUsers_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmUsersList"))
            {
                frmUsersList f = new frmUsersList();
                f.Show(dockPanel1);
            }
        }

        private void mnuLogin_Click(object sender, EventArgs e)
        {
            Program.isAdmin = false;

            frmLogin f = new frmLogin();
            DialogResult fValue = f.ShowDialog(this);
            if (fValue == DialogResult.OK)
            {
                foreach (Form fChild in this.MdiChildren)
                {
                    fChild.Close();
                }
                AuthenticUser();
                lklDHDB_Click(null, null);
                panel1.Visible = true;
                panel2.Visible = true; cboStatus.SelectedIndex = 0;
            }
            else
            {
                if (fValue == DialogResult.Cancel)
                {
                    Program.isAdmin = false;
                    mnuUsers.Enabled = false;
                    mnuBackup.Enabled = false;
                    mnuChangePassword.Enabled = false;
                    mnuCompany.Enabled = false;

                    tslUsers.Text = "";
                    tslTask.Text = "";
                    panel1.Visible = false;
                    panel2.Visible = false;
                }
            }
        }
        private void frmSystem_Load(object sender, EventArgs e)
        {
            frmLogin f = new frmLogin();
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                AuthenticUser();
                InsertCompany();
                lklDHDB_Click(null, null);
                panel2.Visible = true;
                panel1.Visible = true;
                cboStatus.SelectedIndex = 0;
            }
            else
            {
                mnuUsers.Enabled = false;
                mnuBackup.Enabled = false;
                mnuChangePassword.Enabled = false;
                mnuCompany.Enabled = false;

                tslUsers.Text = "";
                tslTask.Text = "";
                txtFullName.Text = "";
                panel1.Visible = false;
                panel2.Visible = false;
                
            }
        }
        private void AuthenticUser()
        {
            mnuBackup.Enabled= Program.isAdmin;
            tslUsers.Text = "Người dùng: ";
            txtFullName.Text =  UsersDB.GetInfoByUsersName(Program.strUserName).Users_FullName;
            tslTask.Text = "| Quyền: " + UsersDB.GetInfoByUsersName(Program.strUserName).Users_Roles;

            mnuUsers.Enabled = true;
            mnuBackup.Enabled = true;
            mnuChangePassword.Enabled = true;
            mnuCompany.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tslTime.Text = ShowTime();
        }
        private string ShowTime()
        {
            string _DayofWeek = "";
            switch (DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Friday:
                    _DayofWeek = "Thứ sáu";
                    break;
                case DayOfWeek.Monday:
                    _DayofWeek = "Thứ hai";
                    break;
                case DayOfWeek.Saturday:
                    _DayofWeek = "Thứ bảy";
                    break;
                case DayOfWeek.Sunday:
                    _DayofWeek = "Chủ nhật";
                    break;
                case DayOfWeek.Thursday:
                    _DayofWeek = "Thứ năm";
                    break;
                case DayOfWeek.Tuesday:
                    _DayofWeek = "Thứ ba";
                    break;
                case DayOfWeek.Wednesday:
                    _DayofWeek = "Thứ tư";
                    break;
                default:
                    break;
            }
            _DayofWeek = _DayofWeek + " " +
                DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
            
            return _DayofWeek;
        }

        private void InsertCompany()
        {
            CompanyInfo Companyinf = new CompanyInfo();
            Companyinf.Company_Code = "";
            Companyinf.Company_Name = "";
            Companyinf.Company_Address = "";
            Companyinf.Company_Tel = "";
            Companyinf.Company_Fax = "";
            Companyinf.Company_Email = "";
            Companyinf.Company_Website = "";
            Companyinf.Company_Currency = "đồng";
            Companyinf.Company_ID = 1;
            DataTable _dt = CompanyDB.GetOne(1).Tables[0];
            if (_dt.Rows.Count == 0)
            {
                CompanyDB.Insert(Companyinf);
                mnuCompany_Click(null, null);
            }
        }

        private void mnuConfig_Click(object sender, EventArgs e)
        {
            //frmConfig f = new frmConfig();
            ////f.Show(this);
            //DialogResult fValue = f.ShowDialog(this);
            //if (fValue == DialogResult.OK)
            //{
            //    foreach (Form fChild in this.MdiChildren)
            //    {
            //        fChild.Close();
            //    }
            //    AuthenticUser();
            //    lklDHDB_Click(null, null);
            //    panel1.Visible = true;
            //    panel2.Visible = true; cboStatus.SelectedIndex = 0;
            //}
        }

        private void mnuChangePassword_Click(object sender, EventArgs e)
        {
            frmChangePassword f = new frmChangePassword();
            f.Show(this);

        }

        private void lklPXK_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmShipmentList"))
            {
                frmShipmentList f = new frmShipmentList();
                f.Show(dockPanel1);
            }
        }

        private void lklPT_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmIncomeList"))
            {
                frmIncomeList f = new frmIncomeList();
                f.Show(dockPanel1);
            }
        }

        private void lklCNKH_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmCustomerDebitList"))
            {
                frmCustomerDebitList f = new frmCustomerDebitList();
                f.Show(dockPanel1);
            }
        }

        private void lklDHDB_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmOderSalesList"))
            {
                frmOderSalesList f = new frmOderSalesList();
                f.Show(dockPanel1);
            }
        }

        private void lklPNK_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmReceiptList"))
            {
                frmReceiptList f = new frmReceiptList();
                f.Show(dockPanel1);
            }
        }

        private void lklPC_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmSpendList"))
            {
                frmSpendList f = new frmSpendList();
                f.Show(dockPanel1);
            }
        }

        private void lklCNNCC_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmProviderDebitList"))
            {
                frmProviderDebitList f = new frmProviderDebitList();
                f.Show(dockPanel1);
            }
        }

        private void lklDHDM_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmOderPurchaseList"))
            {
                frmOderPurchaseList f = new frmOderPurchaseList();
                f.Show(dockPanel1);
            }
        }
        private void lklTTK_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmStoreStatusList"))
            {
                frmStoreStatusList f = new frmStoreStatusList(false, "");
                f.Show(dockPanel1);
            }
        }
        private void lklMH_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmItemList"))
            {
                frmItemList f = new frmItemList(false,"");
                f.Show(dockPanel1);
            }
        }

        private void lklKH_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmCustomerList"))
            {
                frmCustomerList f = new frmCustomerList(false);
                f.Show(dockPanel1);
            }
        }

        private void lklNCC_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmProviderList"))
            {
                frmProviderList f = new frmProviderList(false);
                f.Show(dockPanel1);
            }
        }

        private void lklLKH_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmCustomerTypeList"))
            {
                frmCustomerTypeList f = new frmCustomerTypeList();
                f.Show(dockPanel1);
            }
        }

        private void lklXX_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmOriginList"))
            {
                frmOriginList f = new frmOriginList();
                f.Show(dockPanel1);
            }
        }

        private void lklDVT_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmCalculationUnitList"))
            {
                frmCalculationUnitList f = new frmCalculationUnitList();
                f.Show(dockPanel1);
            }
        }



        private void lklPXK_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklPXK.LinkColor = System.Drawing.Color.Tomato;
            this.lklPXK.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }
        private void lklPXK_MouseLeave(object sender, EventArgs e)
        {
            this.lklPXK.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklPXK.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklPT_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklPT.LinkColor = System.Drawing.Color.Tomato;
            this.lklPT.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklPT_MouseLeave(object sender, EventArgs e)
        {
            this.lklPT.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklPT.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklCNKH_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklCNKH.LinkColor = System.Drawing.Color.Tomato;
            this.lklCNKH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklCNKH_MouseLeave(object sender, EventArgs e)
        {
            this.lklCNKH.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklCNKH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklDHDB_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklDHDB.LinkColor = System.Drawing.Color.Tomato;
            this.lklDHDB.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklDHDB_MouseLeave(object sender, EventArgs e)
        {
            this.lklDHDB.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklDHDB.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklPNK_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklPNK.LinkColor = System.Drawing.Color.Tomato;
            this.lklPNK.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklPNK_MouseLeave(object sender, EventArgs e)
        {
            this.lklPNK.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklPNK.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklPC_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklPC.LinkColor = System.Drawing.Color.Tomato;
            this.lklPC.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklPC_MouseLeave(object sender, EventArgs e)
        {
            this.lklPC.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklPC.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklCNNCC_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklCNNCC.LinkColor = System.Drawing.Color.Tomato;
            this.lklCNNCC.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklCNNCC_MouseLeave(object sender, EventArgs e)
        {
            this.lklCNNCC.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklCNNCC.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklDHDM_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklDHDM.LinkColor = System.Drawing.Color.Tomato;
            this.lklDHDM.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklDHDM_MouseLeave(object sender, EventArgs e)
        {
            this.lklDHDM.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklDHDM.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }


        private void lklMH_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklMH.LinkColor = System.Drawing.Color.Tomato;
            this.lklMH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklMH_MouseLeave(object sender, EventArgs e)
        {
            this.lklMH.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklMH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklKH_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklKH.LinkColor = System.Drawing.Color.Tomato;
            this.lklKH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklKH_MouseLeave(object sender, EventArgs e)
        {
            this.lklKH.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklKH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklNCC_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklNCC.LinkColor = System.Drawing.Color.Tomato;
            this.lklNCC.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklNCC_MouseLeave(object sender, EventArgs e)
        {
            this.lklNCC.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklNCC.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklLKH_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklLKH.LinkColor = System.Drawing.Color.Tomato;
            this.lklLKH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklLKH_MouseLeave(object sender, EventArgs e)
        {
            this.lklLKH.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklLKH.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklXX_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklXX.LinkColor = System.Drawing.Color.Tomato;
            this.lklXX.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklXX_MouseLeave(object sender, EventArgs e)
        {
            this.lklXX.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklXX.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklDVT_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklDVT.LinkColor = System.Drawing.Color.Tomato;
            this.lklDVT.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklDVT_MouseLeave(object sender, EventArgs e)
        {
            this.lklDVT.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklDVT.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }
        private void lklGotBack_MouseLeave(object sender, EventArgs e)
        {
            this.lklGotBack.LinkColor = System.Drawing.Color.SteelBlue;
            this.lklGotBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }

        private void lklGotBack_MouseMove(object sender, MouseEventArgs e)
        {
            this.lklGotBack.LinkColor = System.Drawing.Color.Tomato;
            this.lklGotBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        }
        #region StoreStatus
        int pageSize = 10;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "Item_Code", "Mã số MH", "", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_Name", "Tên MH", "", 200, true, true);
            UISetting.AddColumn(ref dgvList1, "Quantity", "SL", "R", 60, true, true);
            UISetting.AddColumn(ref dgvList1, "CalculationUnit_Name", "ĐVT", "C", 90, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_PurchasePrice", "Giá mua", "R", 110, false, true);
            UISetting.AddColumn(ref dgvList1, "Item_WholesalePrice", "Giá BB", "R", 110, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_RetailPrice", "Giá BL", "R", 110, true, true);
            UISetting.AddColumn(ref dgvList1, "Origin_Name", "Xuất xứ", "C", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Total", "Tổng trị giá", "R", 130, true, true);

        }
        private void LoadData()
        {
            string keyWord = "";
            if (txtKeyWord.Text.Trim().Contains("'"))
                keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                keyWord = txtKeyWord.Text.Trim();


            DataTable dt = new DataTable();
            if (cboStatus.SelectedIndex == 0)//Đang tồn
                dt = InventoryDB.GetList(out itemCount, keyWord, currPage, pageSize).Tables[0];
            if (cboStatus.SelectedIndex == 1)//Đã nhập
                dt = ReceiptDetailsDB.GetList(out itemCount, keyWord, currPage, pageSize).Tables[0];
            if (cboStatus.SelectedIndex == 2)//Đã Xuất
                dt = ShipmentDetailsDB.GetList(out itemCount, keyWord, currPage, pageSize).Tables[0];

            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);

                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                dgvList1.Rows[i].Cells["Item_ID"].Value = dt.Rows[i]["Item_ID"].ToString();
                dgvList1.Rows[i].Cells["Item_Code"].Value = dt.Rows[i]["Item_Code"].ToString();
                dgvList1.Rows[i].Cells["Item_Name"].Value = dt.Rows[i]["Item_Name"].ToString();
                dgvList1.Rows[i].Cells["CalculationUnit_Name"].Value = dt.Rows[i]["CalculationUnit_Name"].ToString();
                dgvList1.Rows[i].Cells["Item_PurchasePrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_PurchasePrice"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Item_WholesalePrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_WholesalePrice"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Item_RetailPrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_RetailPrice"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Origin_Name"].Value = dt.Rows[i]["Origin_Name"].ToString();


                if (cboStatus.SelectedIndex == 0 && dt.Rows[i]["Quantity"] == DBNull.Value)
                {
                    dgvList1.Rows[i].Cells["Quantity"].Value = dt.Rows[i]["ReceiptQuantity"].ToString();
                }
                else
                    dgvList1.Rows[i].Cells["Quantity"].Value = dt.Rows[i]["Quantity"].ToString();

                if (cboStatus.SelectedIndex == 0 && dt.Rows[i]["Total"] == DBNull.Value)
                {
                    dgvList1.Rows[i].Cells["Total"].Value = Convert.ToDecimal(dt.Rows[i]["ReceiptTotal"]).ToString("#,##0");
                }
                else
                    dgvList1.Rows[i].Cells["Total"].Value = Convert.ToDecimal(dt.Rows[i]["Total"]).ToString("#,##0");

            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        #endregion

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //Interval = 300000 (1 second = 1000, 60 second = 60000, 300 second = 300000)
            btnRefresh_Click(null, null);
        }

        private void mnuRestore_Click(object sender, EventArgs e)
        {
            frmRestore f = new frmRestore();
            f.Show();
            
        }

        private void lklGotBack_Click(object sender, EventArgs e)
        {
            if (!findMdiChildren("frmGotBackList"))
            {
                frmGotBackList f = new frmGotBackList();
                f.Show(dockPanel1);
            }
        }

        private void mnuUserGuide_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, helpProvider1.HelpNamespace);
        }


        private void mnuBackup_Click(object sender, EventArgs e)
        {
            frmBackup f = new frmBackup();
            f.Show();

        }



    }
    

    
}